package com.yueya.basic.notice.action;

import java.util.HashMap;
import java.util.Map;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.notice.model.Notice;
import com.yueya.basic.notice.model.NoticeStatus;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;

/**
 * 通知公告查询
 * 
 * @author yangfeng
 * 
 */
public class NoticeQueryController extends BaseController {

	@SuppressWarnings("rawtypes")
	Map map = new HashMap();

	public void index() {
	}

	public void checklist() {// 通知公告
		this.setAttr("flag", "2");
		render("/WEB-INF/views/basic/notice/check_list.jsp");
	}

	public void portlet() {
		render("/WEB-INF/views/basic/notice/portlet.jsp");
	}

	public void view() {
		String id = this.getPara("id");

		Record notice = Notice.service.getNoticeById(id);
		setAttr("tzgg", notice);
		render("/WEB-INF/views/basic/notice/view.jsp");
	}

	@SuppressWarnings("unchecked")
	public void getListPager() {
		String userid = getUserWithCookie().getStr("user_id");
		String name = "";
		map.clear();
		if (this.getPara("name") == null || this.getPara("name").equals("")) {
			name = "";
		} else {
			name = this.getPara("name").trim();
		}
		map.put("name", name);
		map.put("status", "1");
		map.put("userid", userid);
		Page<Record> page = Notice.service.pagination(getPage(), getRows(), map, true);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void saveReadStatus() {
		String userid = getUserWithCookie().getStr("user_id");
		String id = this.getPara("id");

		boolean bl = NoticeStatus.dao.saveReadStatus(id, userid);
		/*
		 * NoticeStatus ts = NoticeStatus.dao
		 * .findFirst("select * from  t_notice_status where notice_id = '" + id +
		 * "' and user_id = '" + userid + "'"); boolean bl = true; if (ts == null) { ts
		 * = new NoticeStatus(); ts.set("id", UUIDGenerator.getUUID());
		 * ts.set("user_id", userid); ts.set("notice_id", id); ts.set("create_time",
		 * DateUtil.getDate(DateUtil.timeFormat)); bl = ts.save(); }
		 */

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("阅读状态保存成功");
		} else {
			j.setSuccess(false);
			j.setMsg("阅读状态保存失败");
		}
		this.renderJson(j);
	}
}
